<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="日期" prop="startTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyyMMdd"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="课程名称" prop="courseNumber">
        <SearchCourseName ref="SearchCourseName" @update="updateCourseInfo" />
      </el-form-item>
      <!-- <el-form-item label="教师名称" prop="teacherNumber">
        <el-select v-model="queryParams.teacherNumber" placeholder="请选择教师编号" clearable size="small">
          <el-option label="请选择字典生成" value="11111" />
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item label="课程状态" prop="courseState">
        <el-select v-model="queryParams.courseState" placeholder="请选择课程状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_work_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select> 
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['school:schedule:add']"
        >新增</el-button> 
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['school:schedule:edit']"
        >修改</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['school:schedule:remove']"
        >删除</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['school:schedule:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="scheduleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="课程名称" align="center" prop="courseName" />
      <el-table-column label="教师名称" align="center" prop="teacherName" />
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <!-- <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{m}') }}</span>
        </template> -->
      </el-table-column>
       <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <!-- <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d} {h}:{m}') }}</span>
        </template> -->
      </el-table-column>
      
      <el-table-column label="课程状态" align="center" prop="courseState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_work_type" :value="scope.row.courseState"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['school:schedule:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['school:schedule:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改课程日期对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="课程名称" prop="courseNumber">
          <SearchCourseName ref="SearchCourseName1" @update="updateCourseInfo1" />
        </el-form-item>
       <el-form-item label="开始时间" prop="startTime">
          <el-date-picker
            v-model="form.startTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            :default-time="['09:30:00']"
            placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker
            v-model="form.endTime"
            type="datetime"
            :default-time="['11:30:00']"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="教师名称" prop="teacherName">
          <SearchTeacher ref="SearchTeacherName" @change="updateTeacherInfo" />
          <!-- <el-select v-model="form.teacherName" placeholder="请选择教师名称">
            <el-option label="请选择字典生成" value="" />
          </el-select> -->
        </el-form-item>
        
        <el-form-item label="课程状态" prop="courseState">
          <el-select v-model="form.courseState" placeholder="请选择课程状态">
            <el-option
              v-for="dict in dict.type.sys_work_type"
              :key="Number(dict.value)"
              :label="dict.label"
              :value="Number(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSchedule, getSchedule, delSchedule, addSchedule, updateSchedule, exportSchedule } from "@/api/school/schedule";
import SearchCourseName from '../model/searchCourseName.vue'
import SearchTeacher from '../model/searchTeacher.vue'

export default {
  name: "Schedule",
  dicts: ['sys_work_type'],
  components: {SearchCourseName, SearchTeacher},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课程日期表格数据
      scheduleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        date: null,
        courseNumber: null,
        teacherNumber: null,
        courseState: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  watch: {
    open: function(newV, oldV) {
      if (newV) {
        console.log(this.form, 'form')
          this.$nextTick(() => {
            if (this.title.search(/修改/g) > -1 ) {
              this.$refs.SearchCourseName1.init(this.form.courseName)
              this.$refs.SearchTeacherName.init(this.form.teacherId)

            } else {
              this.$refs.SearchCourseName1.init()
              this.$refs.SearchTeacherName.init()
            }
          })
      }
    }
  },
  methods: {
    updateCourseInfo(e) {
      this.queryParams.courseNumber = e
    },
    updateCourseInfo1(e) {
      this.form.courseNumber = e
    },
    updateTeacherInfo(e) {
      this.form.teacherNumber = e
    },
    /** 查询课程日期列表 */
    getList() {
      this.loading = true;
      listSchedule(this.queryParams).then(response => {
        this.scheduleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        date: null,
        startTime: null,
        endTime: null,
        courseNumber: null,
        teacherNumber: null,
        courseState: null,
        nextDate: null,
        nextStartTime: null,
        nextEndTime: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
     // 查看详情
    handleDetail(row) {
      console.log(row.id)
      this.$router.push({path: `school/schedule/detail?id=${row.id}&courseNumber=${row.courseNumber}&startTime=${row.startTime}&endTime=${row.endTime}&teacherNumber=${row.teacherNumber}`})
    }, 
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.$refs.SearchCourseName.init()
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加课程日期";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSchedule(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改课程日期";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSchedule(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSchedule(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除课程日期编号为"' + ids + '"的数据项？').then(function() {
        return delSchedule(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有课程日期数据项？').then(() => {
        this.exportLoading = true;
        return exportSchedule(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
